package cn.yili12.mq;

import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.xbean.BrokerFactoryBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

import java.net.URI;

@SpringBootApplication
@Slf4j
public class MQApplication implements CommandLineRunner {


    public static void main(String[] args) {
        SpringApplication.run(MQApplication.class, args);
    }

    @Bean
    public BrokerFactoryBean activemq() throws Exception {
        BrokerFactoryBean broker = new BrokerFactoryBean();
        broker.setConfig(new ClassPathResource("activemq.xml"));
        broker.setStart(true);
        return broker;
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("启动中");
    }

    private void startBroker() throws Exception {
        BrokerService brokerService = new BrokerService();
        brokerService.setUseJmx(true);
        brokerService.addConnector("tcp://0.0.0.0:61616");
        brokerService.start();
    }

    private void startBroker1() throws Exception {
        String uri = "properties:broker.properties";
        BrokerService brokerService = BrokerFactory.createBroker(new URI(uri));
        brokerService.addConnector("tcp://0.0.0.0:61616");
        brokerService.start();
    }
}
